package com.gtt.lazy.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.gtt.lazy.domain.ColorCategory;
import com.gtt.lazy.domain.Colors;
import com.gtt.lazy.service.ColorCategoryService;
import com.gtt.lazy.service.ColorService;

/**
 * 调色板Action
 * 
 * @author 高甜甜
 * @date 2009-7-15
 */
public class ColorAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ColorService colorService;
	private ColorCategoryService colorCategoryService;

	private List<Colors> colors;

	private int id;
	private String name;
	private int recommend;
	private int ccatid;

	private File upload;
	private String contentType;
	private String fileName;

	private InputStream imageStream;

	private List<ColorCategory> ccs;

	@SuppressWarnings("unchecked")
	public String list() {
		// 确定所要显示的页数
		ccs = colorCategoryService.list();
		if (ccatid == 0)
			ccatid = ccs.get(0).getId();
		getSession().put("ccatid", ccatid);

		if (page == 0)
			page = 1;
		int start = (page - 1) * 21;
		colors = colorService.list(ccatid, start, 21);
		setPagination(colorService.count(ccatid), 21, page);
		return SUCCESS;
	}

	public String toAdd() {
		ccs = colorCategoryService.list();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String add() {
		Colors colors = new Colors(name);
		colors.setRecommend(recommend);
		colors.setCcid(ccatid);
		int id = colorService.add(colors);

		if (upload != null) {
			String image = ImageUtil
					.store(id, fileName, upload, getImagePath());
			colors.setId(id);
			colors.setPath(image);
			colorService.update(colors);
		}
		getSession().put("ccatid", ccatid);
		return SUCCESS;
	}

	public String del() {
		String image = colorService.get(id).getPath();
		colorService.delete(id);
		try {
			if (!StringUtils.isEmpty(image))
				ImageUtil.delete(getImagePath(), image);
		} catch (IOException e) {
		}
		return SUCCESS;
	}

	public String up() {
		Colors color = colorService.get(id);
		if (color.getRecommend() < 5)
			colorService.updateRecommend(id, color.getRecommend() + 1);
		return SUCCESS;
	}

	public String down() {
		Colors color = colorService.get(id);
		if (color.getRecommend() > 1)
			colorService.updateRecommend(id, color.getRecommend() - 1);
		return SUCCESS;
	}

	public String getAttach() {
		Colors color = colorService.get(id);
		this.fileName = color.getPath();
		imageStream = ImageUtil.get(fileName, getImagePath());
		if (imageStream == null) {
			this.fileName = Constant.IMG_ERROR;
			imageStream = ImageUtil
					.get(Constant.IMG_ERROR, getImageErrorPath());
		}
		this.contentType = getMimeType(fileName);
		return SUCCESS;
	}

	/**
	 * 获取图片存储的根路径
	 * 
	 * @return
	 */
	private String getImagePath() {
		return ServletActionContext.getServletContext().getRealPath(
				File.separator + "colors");
	}

	public List<ColorCategory> getCcs() {
		return ccs;
	}

	public void setCcs(List<ColorCategory> ccs) {
		this.ccs = ccs;
	}

	public List<Colors> getColors() {
		return colors;
	}

	public void setColors(List<Colors> colors) {
		this.colors = colors;
	}

	public File getUpload() {
		return upload;
	}

	public InputStream getImageStream() {
		return imageStream;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}

	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getCcatid() {
		return ccatid;
	}

	public void setCcatid(int ccatid) {
		this.ccatid = ccatid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getRecommend() {
		return recommend;
	}

	public void setRecommend(int recommend) {
		this.recommend = recommend;
	}

	public String getContentType() {
		return contentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setColorService(ColorService colorService) {
		this.colorService = colorService;
	}

	public void setColorCategoryService(
			ColorCategoryService colorCategoryService) {
		this.colorCategoryService = colorCategoryService;
	}

}
